import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-03
 * Time: 9:48
 */
public class Delete implements Option {
    @Override
    public void function() throws SQLException {
        //创建数据源对象
        MysqlDataSource source = new MysqlDataSource();
        source.setURL("jdbc:mysql://127.0.0.1:3306/homework?characterEncoding=utf8&useSSL=false");
        source.setUser("root");
        source.setPassword("111111");
        //建立连接
        Connection connection = source.getConnection();
        //sql语句
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的id");
        int id = scanner.nextInt();
        statement.setInt(1, id);
        //发送到服务器
        statement.executeUpdate();
        //关闭资源
        statement.close();
        connection.close();
    }
}
